﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
namespace MP.Core.Authentication.Entity
{
    /// <summary>
    /// Cac gia tri luu session cua user
    /// </summary>
    public class UserSession
    {
        protected string SessionID = "";
        protected String lastFileUpload = "";

        MP.Core.Library.Logger log;
        System.Web.UI.Page page;
        public UserSession(String sessionID, System.Web.UI.Page _page)
        {
            SessionID = sessionID;
            page = _page;
            log = new MP.Core.Library.Logger(this.GetType());
        }
        /// <summary>
        /// Ten file import upload len server
        /// </summary>
        public String LastFileUploadPath
        {
            get { return lastFileUpload; }
            set { lastFileUpload = value; }
        }
        /// <summary>
        /// Upload hinh anh len thu muc tam. 
        /// </summary>
        /// <param name="page"></param>
        /// <param name="fileUpload"></param>
        /// <returns></returns>
        public bool UploadTempImage(System.Web.UI.Page page, FileUpload fileUpload)
        {
            try
            {
                String tempFolder = UserImportPath;
                if (!System.IO.Directory.Exists(tempFolder))
                {
                    System.IO.Directory.CreateDirectory(tempFolder);
                }
                String tempFilePath = tempFolder + GetTempFilePath(fileUpload.FileName);
                fileUpload.SaveAs(tempFilePath);
                LastFileUploadPath = tempFilePath;
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return false;
            }
            return true;
        }
        /// <summary>
        /// Upload cac file import len thu muc tam
        /// </summary>
        /// <param name="page"></param>
        /// <param name="fileUpload"></param>
        /// <returns></returns>
        public string UploadTempImportFile(System.Web.UI.Page page, FileUpload fileUpload)
        {
            try
            {
                String tempFolder = UserImportPath;
                if (!System.IO.Directory.Exists(tempFolder))
                {
                    System.IO.Directory.CreateDirectory(tempFolder);
                }
                String tempFilePath = tempFolder + GetTempFilePath(fileUpload.FileName);
                fileUpload.SaveAs(tempFilePath);
                LastFileUploadPath = tempFilePath;
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return "";
            }
            return LastFileUploadPath;
        }
        /// <summary>
        /// Upload cac file import len thu muc tam
        /// </summary>
        /// <param name="page"></param>
        /// <param name="fileUpload"></param>
        /// <returns></returns>
        public string UploadTempFile(System.Web.UI.Page page, FileUpload fileUpload)
        {
            try
            {
                String tempFolder = UserImportPath;
                if (!System.IO.Directory.Exists(tempFolder))
                {
                    System.IO.Directory.CreateDirectory(tempFolder);
                }
                String tempFilePath = tempFolder + GetTempFilePath(fileUpload.FileName);
                fileUpload.SaveAs(tempFilePath);
                LastFileUploadPath = tempFilePath;
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return "";
            }
            return LastFileUploadPath;
        }


        protected String GetTempFilePath(String fileName)
        {
            return DateTime.Now.ToString("yyyyMMddhhmmss") + fileName;
        }
        /// <summary>
        /// Thu muc chua cac file upload/download tam cua user. Sau khi het session, thu muc nay se bi xoa
        /// </summary>
        public String UserSessionPath
        {
            get
            {

                string path = page.Server.MapPath( "/SessionProfiles/Temp/") + SessionID + "/";
                if (!System.IO.Directory.Exists(path))
                    System.IO.Directory.CreateDirectory(path);
                return path;

            }
        }
        public String UserImageUploadPath
        {
            get
            {

                string path = UserSessionPath + "Images/";
                if (!System.IO.Directory.Exists(path))
                    System.IO.Directory.CreateDirectory(path);
                return path;
            }
        }

        public String UserImportPath
        {
            get
            {

                string path = UserSessionPath + "Import/";
                if (!System.IO.Directory.Exists(path))
                    System.IO.Directory.CreateDirectory(path);
                return path;
            }

        }

        public String UserExportPath
        {
            get
            {

                string path = UserSessionPath + "Export/";
                if (!System.IO.Directory.Exists(path))
                    System.IO.Directory.CreateDirectory(path);
                return path;
            }

        }

    }
}
